package backup

import (
	"phant-operator/pkg/backup"
	"phant-operator/pkg/client/clientset/versioned"

	batchv1 "k8s.io/api/batch/v1"
)

// ControlInterface implements the control logic for updating Backup
// It is implemented as an interface to allow for extensions that provide different semantics.
// Currently, there is only one implementation.
type ControlInterface interface {
	// UpdateBackup implements the control logic for backup job and backup clean job's creation, deletion
	Update(backupJobName string, job *batchv1.Job) error
}

type defaultBackupControl struct {
	cli           versioned.Interface
	backupManager backup.BackupManager
}

// NewDefaultBackupControl returns a new instance of the default implementation BackupControlInterface that
// implements the documented semantics for Backup.
func NewDefaultBackupControl(
	cli versioned.Interface,
	backupManager backup.BackupManager) ControlInterface {
	return &defaultBackupControl{
		cli,
		backupManager,
	}
}

// UpdateBackup executes the core logic loop for a Backup.
func (c *defaultBackupControl) Update(backupJobName string, job *batchv1.Job) error {
	//backup.SetGroupVersionKind(controller.BackupControllerKind)

	return c.updateBackup(backupJobName, job)
}

func (c *defaultBackupControl) updateBackup(backupJobName string, job *batchv1.Job) error {
	return c.backupManager.Sync(backupJobName, job)
}
